/*
*  Copyright 2019-2020 dhm
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.admin.qujiang.service.impl;

import com.admin.qujiang.model.YlCodeFileModel;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.admin.qujiang.service.IYlCodeFileService;
import com.admin.qujiang.dto.YlCodeFileDto;
import com.admin.qujiang.service.dto.YlCodeFileQueryCriteria;
import com.admin.qujiang.mapper.YlCodeFileMapper;
import com.admin.utils.DozerUtils;
import org.dozer.Mapper;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.util.IdUtil;
import me.zhengjie.utils.PageUtil;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import org.springframework.data.domain.Pageable;
import java.util.*;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author dhm
* @date 2022-05-31
**/
@Service
@RequiredArgsConstructor
public class YlCodeFileServiceImpl extends ServiceImpl<YlCodeFileMapper, YlCodeFileModel> implements IYlCodeFileService {

    // dozer做Model与DTO的转换
    private final Mapper mapper;
    
    private QueryWrapper<YlCodeFileModel> buildWrapper(YlCodeFileQueryCriteria criteria, Boolean isQuery) {
        QueryWrapper<YlCodeFileModel> query = null;
        if (null != criteria) {
            query = new QueryWrapper<YlCodeFileModel>();
        }
        return query;
    }

    @Override
    public List<YlCodeFileDto> queryAll(YlCodeFileQueryCriteria criteria, Boolean isQuery){
        List<YlCodeFileModel> ylCodeFiles = this.list(buildWrapper(criteria, isQuery));
        return DozerUtils.mapList(mapper, ylCodeFiles, YlCodeFileDto.class);
    }

    @Override
    @Transactional
    public YlCodeFileDto findById(String id) {
        YlCodeFileModel ylCodeFile = Optional.ofNullable(this.getById(id)).orElseGet(YlCodeFileModel::new);
        ValidationUtil.isNull(ylCodeFile.getId(),"YlCodeFile","id",id);
        return mapper.map(ylCodeFile, YlCodeFileDto.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public YlCodeFileModel create(YlCodeFileModel resources) {
        resources.setId(IdUtil.simpleUUID()); 
        this.save(resources);
        return resources;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(YlCodeFileModel resources) {
        YlCodeFileModel ylCodeFile = Optional.ofNullable(this.getById(resources.getId())).orElseGet(YlCodeFileModel::new);
        ValidationUtil.isNull(ylCodeFile.getId(), "YlCodeFile", "id", resources.getId());
        ylCodeFile = mapper.map(resources, YlCodeFileModel.class);
        this.saveOrUpdate(ylCodeFile);
    }

    @Override
    public void deleteAll(String[] ids) {
        this.removeByIds(Arrays.asList(ids));
    }

    @Override
    public void download(List<YlCodeFileDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (YlCodeFileDto ylCodeFile : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put(" name",  ylCodeFile.getName());
            map.put(" url",  ylCodeFile.getUrl());
            map.put(" fileType",  ylCodeFile.getFileType());
            map.put(" eventId",  ylCodeFile.getEventId());
            map.put(" addTime",  ylCodeFile.getAddTime());
            map.put(" status",  ylCodeFile.getStatus());
            map.put(" updateTime",  ylCodeFile.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}